package com.vueapi.controller.admin;

import com.vueapi.annotation.OperationLog;
import com.vueapi.constant.ErrorCodeEnum;
import com.vueapi.domain.HjUser;
import com.vueapi.exception.UserException;
import com.vueapi.service.HjUserService;
import com.vueapi.utils.JwtUtil;
import com.vueapi.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname AdminLoginController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/21 9:18
 * @Version 1.0.0
 */
@RequestMapping("/admin/login")
@RestController
public class AdminLoginController {

    @Autowired
    private HjUserService userService;

    @OperationLog(name = "用户登录")
    @PostMapping
    public ResultUtils login(@RequestBody HjUser user) {
        if (user == null) {
            throw new UserException(ErrorCodeEnum.ACCOUNT_PWD_ERROR.getCode(), ErrorCodeEnum.ACCOUNT_PWD_ERROR.getMsg());
        }
        if (user.getUsername() == null || "".equals(user.getUsername())) {
            throw new UserException(ErrorCodeEnum.ACCOUNT_PWD_ERROR.getCode(), ErrorCodeEnum.ACCOUNT_PWD_ERROR.getMsg());
        }
        if (user.getPassword() == null || "".equals(user.getPassword())) {
            throw new UserException(ErrorCodeEnum.ACCOUNT_PWD_ERROR.getCode(), ErrorCodeEnum.ACCOUNT_PWD_ERROR.getMsg());
        }
        String token = userService.login(user);
        if(token == null){
            return ResultUtils.fail("用户名或密码错误");
        }
        return new ResultUtils(4624,"登录成功",token);
    }

}
